// pages/plan/JiHuaXuFei.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active:'',
    imgPath:'/images',
    price:368,
    text1:'你的计划将于2022年3月18日到期',
    cardOnsale:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const util=require("../../utils/util")
    this.setData({
      imgPath:util.getImgPath()
    })

    wx.setNavigationBarTitle({
      title: '计划续贯',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
         
          const that = this;
          //当前在售成长卡
          const api = require("../../api/api");
          api.card.cardOnsale({
            success(res) {
              // console.log("cardOnsale,res", res)
              that.setData({
                cardOnsale: res.data.data
              })
             console.log("that.data.cardOnsale",that.data.cardOnsale)
            }
          })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onTabbarChange(event){
    // console.log("daoQi,onTabbarChange,event",event)
    this.setData({
      active:event.detail
    })
  },
  liJiXuFei(){
    wx.showLoading({title: '加载中', icon: 'loading'});
    // wx.showToast({
    //   title: '立即续费'+this.data.price,
    // })
    const api = require("../../api/api")
    const util=require("../../utils/util");
    api.order.transactions({
      goods_info: [{
        goods_id: this.data.cardOnsale.id,
        buy_number: 1,
        coupon_id: "",
        channel:util.getChannel(),
      }],
      price: this.data.cardOnsale.price,
      success(res) {
        wx.hideLoading();
        // console.log("transactions.res", res)
        let order_sn = res.data.data.order_sn;
        api.order.payment({
          order_sn: order_sn,
          pay_type: 'wx',
          pay_client: 'lite',
          success(resPayment) {
            // console.log("payment,resPayment", resPayment)

            let dataPayment = resPayment.data.data;

            //微信支付
            wx.requestPayment({
              timeStamp: dataPayment.timeStamp,
              nonceStr: dataPayment.nonceStr,
              package: dataPayment.package,
              signType: 'MD5',
              paySign: dataPayment.paySign,
              success(resPay) {
                // console.log("wx.requestPayment,success,resPay", resPay)
                
                //支付成功
                // wx.navigateTo({
                //   url: '/pages/orderSheet/paySuccess',
                // })
                wx.navigateTo({
                  url: '/pages/index/index?query=paySuccess',
                })
              },
              fail(resPay) {
                // console.log("wx.requestPayment,fail,resPay", resPay);
                //支付失败

                //调订单删除接口
                api.user.orderDelete({
                  order_sn: order_sn,
                  success(resDel){
                    // console.log("orderDelete,resDel",resDel)
                  }
                })

                wx.navigateTo({
                  url: '/pages/orderSheet/payFail?jp=/pages/plan/joinPlan',
                })
              }
            })
          }
        })
      }
    })
    // wx.navigateTo({
    //   url: '/pages/orderSheet/paying',
    // })
  }
})